package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.ProductIn;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.entity.Schedule;
import com.woniuxy.factory.mapper.ProductInMapper;
import com.woniuxy.factory.mapper.ScheduleMapper;
import com.woniuxy.factory.service.ProductInService;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-22 17:54
 */
@Service
public class ProductInServiceImp implements ProductInService {

    @Autowired
    ProductInMapper productInMapper;

    //生产进度
    @Autowired
    ScheduleMapper scheduleMapper;

    @Override
    public List<ProductIn> selectAllProductIn(Integer page, Integer limit, ProductIn productIn) {

        //调用插入产品入库的记录表
        insertProductIn();

        //查找所有的产品入库表
        int home = (page-1)*limit;
        List<ProductIn> productIns = productInMapper.selelectAllPrudectIn(home, limit, productIn);
        System.out.println("seivice层长度："+productIns.size());
        return productIns;
    }

    @Override
    public ResponseResult updateRealCount(ProductIn productIn,String serialNumber) {
        ResponseResult responseResult = new ResponseResult();

        //设置入库时间并更改状态为完成
        productIn.setProductintime(new Date());
        productIn.setProductinstatus(AuditType.ALREADY_PASSED);

        try {
            productInMapper.updateRealCount(productIn,serialNumber);
        }catch (RuntimeException e){
            responseResult.setMsg("更新错误,请检查格式");
        }

        responseResult.setMsg("确认入库成功");
        responseResult.setCode(AuditType.No_Approval);
        return responseResult;
    }

    @Override
    public int selectAllCount() {
        int count = productInMapper.selectAllCount();
        return count;
    }


    //插入产品入库记录
    public void insertProductIn(){
        Schedule schedule = new Schedule();
        schedule.setState(AuditType.ALREADY_PASSED);
        //查找所有状态为完成的生产进度单
        List<Schedule> schedules = scheduleMapper.selectScheduleAchieve(schedule);
        //非空验证
        if(schedules.size()>0&&!schedules.isEmpty()) {
            for (Schedule schedule1 : schedules) {
                //判断当前生产进度是否在产品入库表中存在
                int count = productInMapper.selectCountBySchId(schedule1.getSerialNumber());
                if (count == 0) {
                    productInMapper.insertProductIn(schedule1.getSerialNumber());
                }
            }
        }
    }
}
